﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DTO;
namespace DAL
{
    public class ADMINDAL: KetNoiDuLieu
    {
        public void Insert(ADMIN ad)
        {
            SqlCommand cm = new SqlCommand("sp_admin_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@USER_NAME", ad.Username);
            cm.Parameters.AddWithValue("@PASS", ad.Pass);
            cm.Parameters.AddWithValue("@QUYEN", ad.Quyen);
            cm.Parameters.AddWithValue("@HO_TEN", ad.HoTen);
            cm.Parameters.AddWithValue("@EMAIL", ad.Mail);
            cm.Parameters.AddWithValue("@NGAY_TAO", ad.Ngaytao);
            cm.Parameters.AddWithValue("@TRANG_THAI", ad.Trangthai);
            cm.ExecuteNonQuery();

        }
        public void Update(ADMIN ad)
        {
            SqlCommand cm = new SqlCommand("sp_admin_Update", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_AD",ad.Ma_ad);
            cm.Parameters.AddWithValue("@USER_NAME", ad.Username);
            cm.Parameters.AddWithValue("@PASS", ad.Pass);
            cm.Parameters.AddWithValue("@QUYEN", ad.Quyen);
            cm.Parameters.AddWithValue("@HO_TEN", ad.HoTen);
            cm.Parameters.AddWithValue("@EMAIL", ad.Mail);
            cm.Parameters.AddWithValue("@NGAY_TAO", ad.Ngaytao);
            cm.Parameters.AddWithValue("@TRANG_THAI", ad.Trangthai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int ma_ad)
        {
            SqlCommand cm = new SqlCommand("sp_admin_Delete", LayKetNoi());

            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_AD", ma_ad);
           
            cm.ExecuteNonQuery();
        }
        public AdminColection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                        string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_admin_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            AdminColection adcl = new AdminColection();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ADMIN admin = new ADMIN();
                admin.Ma_ad = int.Parse(ds.Tables[0].Rows[i]["MA_AD"].ToString());
                admin.Username = ds.Tables[0].Rows[i]["USER_NAME"].ToString();
                admin.Pass = ds.Tables[0].Rows[i]["PASS"].ToString();
                admin.Quyen = ds.Tables[0].Rows[i]["QUYEN"].ToString();
                admin.HoTen = ds.Tables[0].Rows[i]["HO_TEN"].ToString();
                admin.Mail = ds.Tables[0].Rows[i]["EMAIL"].ToString();
                admin.Ngaytao = ds.Tables[0].Rows[i]["NGAY_TAO"].ToString();
                admin.Trangthai = int.Parse(ds.Tables[0].Rows[i]["TRANG_THAI"].ToString());
                adcl.Add(admin);

            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return adcl;
        }
     
      
    }
}
